package com.qt.service.oj.impl;

import cn.hutool.core.util.IdUtil;
import com.qt.pojo.vo.CaptchaVO;
import com.qt.result.R;
import com.qt.service.oj.CommonService;
import com.qt.utils.RedisUtil;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/23 16:55
 */
@Service
public class CommonServiceImpl  implements CommonService {

    @Autowired
    private RedisUtil redisUtil;


    @Override
    public R getCaptcha() {
        ArithmeticCaptcha specCaptcha = new ArithmeticCaptcha(90, 30, 4);
        specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
        // 2位数运算
        specCaptcha.setLen(2);
        String verCode = specCaptcha.text().toLowerCase();
        String key = IdUtil.simpleUUID();
        // 存入redis并设置过期时间为30分钟
        redisUtil.set(key, verCode, 1800);
        // 将key和base64返回给前端
        CaptchaVO captchaVo = new CaptchaVO();
        captchaVo.setImg(specCaptcha.toBase64());
        captchaVo.setCaptchaKey(key);

        return R.success(captchaVo);
    }
}
